﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.IO;
using Lite.ExcelLibrary.SpreadSheet;
using System.Collections;
using System.Xml.Linq;
using System.Runtime.InteropServices.Automation;
using System.Collections.ObjectModel;
using System.Windows.Browser;

namespace AdvancedStandingAndScholarship
{
    public partial class AccountManagementPage : Page
    {
        public AccountManagementPage()
        {
            InitializeComponent();
        }

        public static List<ASASService.User> userList = new List<ASASService.User>();

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ASASService.ASASServiceClient client = new ASASService.ASASServiceClient();

            client.getPendingUserCompleted += new EventHandler<ASASService.getPendingUserCompletedEventArgs>(client_getPendingUserCompleted);
            client.getPendingUserAsync();

            client.getAllUserCompleted += new EventHandler<ASASService.getAllUserCompletedEventArgs>(client_getAllUserCompleted);
            client.getAllUserAsync();

        }

        void client_getPendingUserCompleted(object sender, ASASService.getPendingUserCompletedEventArgs e)
        {
            if (e.Error == null & e.Result != null)
            {
                requestDataGrid.ItemsSource = (List<ASASService.User>)e.Result;
            }
        }

        void client_getAllUserCompleted(object sender, ASASService.getAllUserCompletedEventArgs e)
        {
            if (e.Error == null & e.Result != null)
            {
                adminDataGrid.ItemsSource = (List<ASASService.User>)e.Result;
                
                
            }
        }

        private void btnAddPassword_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            gridPassword.Visibility = Visibility.Visible;
            gridAdminManagement.Visibility = Visibility.Collapsed;
            gridRequestApproval.Visibility = Visibility.Collapsed;

            lblAdminManagement.Foreground = new SolidColorBrush(Color.FromArgb(163, 163, 163, 163));
            lblChangePw.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
            lblRequestApproval.Foreground = new SolidColorBrush(Color.FromArgb(163, 163, 163, 163));
        }

     private void btnRequestApproval_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            gridPassword.Visibility = Visibility.Collapsed;
            gridAdminManagement.Visibility = Visibility.Collapsed;
            gridRequestApproval.Visibility = Visibility.Visible;


            lblChangePw.Foreground = new SolidColorBrush(Color.FromArgb(163, 163, 163, 163));
            lblAdminManagement.Foreground = new SolidColorBrush(Color.FromArgb(163, 163, 163, 163));
            lblRequestApproval.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
        }

     private void btnAdminManagement_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
     {
         //// TODO: Add event handler implementation here.
         gridPassword.Visibility = Visibility.Collapsed;
         gridAdminManagement.Visibility = Visibility.Visible;
         gridRequestApproval.Visibility = Visibility.Collapsed;


         lblChangePw.Foreground = new SolidColorBrush(Color.FromArgb(163, 163, 163, 163));
         lblAdminManagement.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
         lblRequestApproval.Foreground = new SolidColorBrush(Color.FromArgb(163, 163, 163, 163));
     }


        private void btnBack_Add_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            if (NavigationService.CanGoBack)
                NavigationService.GoBack();
            else
            {
                NavigationService.Navigate(new Uri("/MainPage", UriKind.Relative));
            }
        }

        private string GetCookie(string key)
        {
            string[] cookies = HtmlPage.Document.Cookies.Split(';');

            foreach (string cookie in cookies)
            {
                string[] keyValue = cookie.Split('=');
                if (keyValue.Length == 2)
                {
                    if (keyValue[0].ToString() == key)
                        return keyValue[1];
                }
            }
            return null;
        }


        public void SetCookie(string key, string value)
        {
            DateTime expireDate = DateTime.Now.Add(TimeSpan.FromMinutes(15));
            string newCookie = String.Format("{0}={1};expires={2}", key, value, expireDate.ToString("R"));
            HtmlPage.Document.SetProperty("cookie", newCookie);
        }

        private void btnSubmit1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            List<ASASService.User> userList = new List<ASASService.User>();
            userList = (List<ASASService.User>)adminDataGrid.ItemsSource;
            List<ASASService.User> userListToDelete = new List<ASASService.User>();
            foreach (KeyValuePair<string, bool> temp in deleteList)
            {
                foreach (ASASService.User user in userList)
                {
                    if (temp.Key == user.Email)
                    {
                        user.ChkDelete = true;
                        break;
                    }
                }
            }

            ASASService.ASASServiceClient client = new ASASService.ASASServiceClient();
            client.updateAndDeleteUserCompleted += new EventHandler<ASASService.updateAndDeleteUserCompletedEventArgs>(client_updateAndDeleteUserCompleted);
            client.updateAndDeleteUserAsync(userList);

        }

        void client_updateAndDeleteUserCompleted(object sender, ASASService.updateAndDeleteUserCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                MessageBox.Show("Update Success!");
                NavigationService.Refresh();
            }

            else
            {
                MessageBox.Show("Update failed.");
            }
        }

        List<KeyValuePair<string, bool>> deleteList = new List<KeyValuePair<string, bool>>();

        private void cbx_delete_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            CheckBox checkbox = sender as CheckBox;
            if (checkbox.IsChecked.Value == false)
            {
                foreach (KeyValuePair<string, bool> temp in deleteList)
                {
                    if (temp.Key == checkbox.Tag.ToString())
                    {
                        deleteList.Remove(temp);
                        break;
                    }
                }
            }
            else
                deleteList.Add(new KeyValuePair<string, bool>(checkbox.Tag.ToString(), checkbox.IsChecked.Value));
        }

        List<KeyValuePair<string, bool>> selectList = new List<KeyValuePair<string, bool>>();

        private void cbx_select_click(object sender, RoutedEventArgs e)
        {
            CheckBox checkbox = sender as CheckBox;
            if (checkbox.IsChecked.Value == false)
            {
                foreach (KeyValuePair<string, bool> temp in selectList)
                {
                    if (temp.Key == checkbox.Tag.ToString())
                    {
                        selectList.Remove(temp);
                        break;
                    }
                }
            }
            else
                selectList.Add(new KeyValuePair<string, bool>(checkbox.Tag.ToString(), checkbox.IsChecked.Value));
        }

        private void btnApprove_Click(object sender, RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            List<ASASService.User> userList = new List<ASASService.User>();
            userList = (List<ASASService.User>)requestDataGrid.ItemsSource;
            List<ASASService.User> userListToDelete = new List<ASASService.User>();
            foreach (KeyValuePair<string, bool> temp in selectList)
            {
                foreach (ASASService.User user in userList)
                {
                    if (temp.Key == user.Email)
                    {
                        user.ChkDelete = true;
                        break;
                    }
                }
            }

            ASASService.ASASServiceClient client = new ASASService.ASASServiceClient();
            client.approveUserCompleted += new EventHandler<ASASService.approveUserCompletedEventArgs>(client_approveUserCompleted);
            client.approveUserAsync(userList);
        }

        void client_approveUserCompleted(object sender, ASASService.approveUserCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                MessageBox.Show("Approve Success!");
                NavigationService.Refresh();
            }

            else
            {
                MessageBox.Show("Approve failed.");
            }
        }

       

    }
}
